import datetime
import logging

import pytest

from resource.pom.settings import Settings
from utils.manager import Direction


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击设置-系统-日期和时间-日期')
        today = datetime.datetime.now()
        device.hdc_shell("date '{}'".format(today.strftime("%Y-%m-%d %H:%M:%S")))
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.dirc_fling(Direction.Down)
        device.click(*self.app.system_item)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('日期和时间'))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('日期'))
        logging.info('预期结果1.可正常进入日期界面')
        device.refresh_layout()
        device.assert_type_exist('Dialog')
        device.assert_type_exist('DatePicker')
        device.assert_text_exist('{}年'.format(today.year))
        device.assert_text_exist('{}月'.format(today.month))
        device.assert_text_exist('{}日'.format(today.day))
        device.assert_text_exist('取消')
        device.assert_text_exist('确定')

        logging.info('步骤2.上下滑动修改“年月日”后点击确定')
        year_area = device.get_element_by_text('{}年'.format(today.year))
        yx, yy = device.center_of_element(year_area)
        month_area = device.get_element_by_text('{}月'.format(today.month))
        mx, my = device.center_of_element(month_area)
        day_area = device.get_element_by_text('{}日'.format(today.day))
        dx, dy = device.center_of_element(day_area)
        device.swipe(yx, yy, yx, yy - 80)
        device.swipe(mx, my, mx, my + 80)
        device.swipe(dx, dy, dx, dy + 80)
        device.wait(1)
        device.click_element(device.get_element_by_text('确定'))
        logging.info('预期结果2.“年月日”上下滑动正常，日期修改生效')
        device.refresh_layout()
        device.assert_text_exist('日期')
        device.assert_text_exist('{}年{}月{}日'.format(today.year, today.month, today.day), expect=False)
